import {
	createRouter,
	createWebHashHistory
} from 'vue-router'
import store from '@/store'

const routes = [
	/************************根路径跳转************************/
	{
		path: '/',
		redirect: '/wechat/studentlogin',
	},
	/*************************微信端开放页面***********************/
	{
		path: '/wechat/studentlogin',
		component: () => import('../views/wechat/index/studentlogin/index'),
		meta: {
			title: "学生授权登录",
			requireAuth: false,
			identity: 0
		}
	},
	{
		path: '/wechat/studenttest',
		component: () => import('../views/wechat/index/studenttest/index'),
		meta: {
			title: "学生测试登录",
			requireAuth: false,
			identity: 0
		}
	},
  /*************************微信端学生页面***********************/
  {
    path: '/wechatstudent/loading',
  	component: ()=>import('../views/wechat/student/loading/index'),
  	meta:{
  		title:"加载页",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/stucenter',
  	component: ()=>import('../views/wechat/student/stucenter/index'),
  	meta:{
  		title:"个人中心",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/battle',
  	component: ()=>import('../views/wechat/student/battle/index'),
  	meta:{
  		title:"校园争霸",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/rankedgame',
  	component: ()=>import('../views/wechat/student/rankedgame/index'),
  	meta:{
  		title:"排位赛",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/forgearea',
  	component: ()=>import('../views/wechat/student/forgearea/index'),
  	meta:{
  		title:"锻造区",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/levelrank',
  	component: ()=>import('../views/wechat/student/levelrank/index'),
  	meta:{
  		title:"排位赛难度",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/studentinfo',
  	component: ()=>import('../views/wechat/student/studentinfo/index'),
  	meta:{
  		title:"欢迎界面",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/knowledge',
  	component: ()=>import('../views/wechat/student/knowledge/index'),
  	meta:{
  		title:"知识升级",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/practice',
  	component: ()=>import('../views/wechat/student/practice/index'),
  	meta:{
  		title:"练功房",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/setup',
  	component: ()=>import('../views/wechat/student/setup/index'),
  	meta:{
  		title:"个人设置",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/ranklist',
  	component: ()=>import('../views/wechat/student/ranklist/index'),
  	meta:{
  		title:"排行榜",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/answer',
  	component: ()=>import('../views/wechat/student/answer/index'),
  	meta:{
  		title:"答题副本",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/balance',
  	component: ()=>import('../views/wechat/student/balance/index'),
  	meta:{
  		title:"结算分数",
  		requireAuth:true,
  		identity:1
  	}
  },
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

/**
 * to:即将要进入的目标对象
 * from:当前导航即将要离开的路由对象
 * next:调用该方法,才能进入下一个钩子,
 * afterEach不需要调用next方法
 */
router.beforeEach((to, from, next) => {
	//从vuex里面准备好身份和token令牌
	var token = store.state.globalData.token;
	var identity = store.state.globalData.identity;
	var expireTime = store.state.globalData.expireTime;
	var nowTime = Date.parse(new Date()) / 1000;
	/* 路由发生变化修改页面title */
	if (to.meta.title) {
		document.title = to.meta.title
	}
	if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
		if (!token) { // 判断本地是否存在token
			// 未登录,跳转到登陆页面
			next({
				path: '/'
			})
			return 0;
		}
		if (nowTime >= expireTime) {
			// 身份信息已经过期,跳转到登陆页面
			next({
				path: '/'
			})
			return 0;
		}
		if (to.meta.identity != 0 && (!identity || identity != to.meta.identity)) {
			// 身份有问题
			next({
				path: '/'
			})
			return 0;
		}
	}
	next();
})

export default router
